/*
 * Copyright (C) 2008-2010 Oliver Horn.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.googlecode.funcj.tuples;

import java.io.Serializable;

import javax.annotation.concurrent.Immutable;

/**
 * Unit is the empty tuple.
 *
 * @see Tuple
 *
 * @author Oliver Horn
 */
@Immutable
public final class Unit implements Tuple, Serializable {

	private static final long serialVersionUID = 3917438544115537436L;

	/**
	 * The singleton instance of this class.
	 */
	public static final Unit UNIT = new Unit();

	private Unit() {
	}

	private Object readResolve() {
		return UNIT;
	}

	/**
	 * {@inheritDoc}
	 * @return always {@code 0}
	 */
	public int arity() {
		return 0;
	}

	/**
	 * {@inheritDoc}
	 * @return an empty array
	 */
	public Object[] toArray() {
		return new Object[0];
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public int hashCode() {
		return 1;
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public boolean equals(Object obj) {
		if (obj == this) {
			return true;
		}
		if (obj instanceof Tuple) {
			return ((Tuple) obj).arity() == 0;
		}
		return false;
	}

	@Override
	public String toString() {
		return "()";
	}
}
